package com.android.picotts.componet;

import java.util.List;

import android.app.Notification;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;

import com.android.picotts.businiss.SpriterManager;
import com.android.picotts.businiss.model.Spriter;
import com.android.picotts.ui.widget.FloatView;
import com.android.picotts.util.Constant;
import com.android.picotts.util.MessageBus;
import com.android.picotts.util.MessageBus.MMessage;
import com.android.picotts.util.MessageBus.UIReceiver;

public class SystemService extends Service{
    
    private static final int NotificationID_TOPLEVLE = 0x111012;  
   
    private FloatView floatView;
    
    private static SystemService instance;
    public static SystemService getInstance() {
        return instance;
    }
    
    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        
        showBackNotification(true);
        MessageBus.getBusFactory().register(Constant.MSG_ADD_SPRITE, mRevicer);
        MessageBus.getBusFactory().register(Constant.MSG_DEL_SPRITE, mRevicer);
        SpriterManager.getInstance(this).load();
        floatView = FloatView.createView();
        
        loadData();
    }
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {        
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        floatView.hideFloatView();
        instance = null;
        MessageBus.getBusFactory().unregister(Constant.MSG_ADD_SPRITE, mRevicer);
        MessageBus.getBusFactory().unregister(Constant.MSG_DEL_SPRITE, mRevicer);
        DamoService.startService(this);
        super.onDestroy();
    }
    
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    
    public void restoreFloat() {
        floatView.closeFloat();
    }
    
    public void setFloatVisible( boolean visible) {
        if(visible) {
            floatView.showFloatView();
        }else {
            floatView.hideFloatView();
        }
    }
    
    private void loadData() {
        List<Spriter> list = SpriterManager.getInstance(this).getList();
        for(Spriter one  : list) {
            floatView.addSpirteView(one);
        }
    }

    private void showBackNotification(boolean backable) {
        if (backable) {
            Notification notification = new Notification(0, null, System.currentTimeMillis());
            notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
            startForeground(NotificationID_TOPLEVLE, notification);
        } else {
            stopForeground(true);
        }
    }
    
    private UIReceiver mRevicer = new UIReceiver() {
        private static final long serialVersionUID = 3450668281193037488L;

        @Override
        public void onReceive(MMessage message) {
            switch(message.what) {
                case Constant.MSG_ADD_SPRITE:
                {
                    Spriter one = (Spriter)message.obj;
                    floatView.addSpirteView(one);
                    break;
                }
                case Constant.MSG_DEL_SPRITE:
                {
                    Spriter one = (Spriter)message.obj;
                    floatView.rmvSpirteView(one);
                    break;
                }
            }
        }
    };
    // //////////////////////////////////////////////////////////////////////////////////
    public static void stopService(Context context) {
         context.stopService(new Intent(context, SystemService.class));
    }

    public static void startService(Context context) {
          context.startService(new Intent(context, SystemService.class));
    }
}